package miage.unice.projet;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.Date;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import miage.unice.menu.Details;
import miage.unice.menu.GrandesMiniatures;
import miage.unice.menu.PetitesMiniatures;


public class Explorateur implements TreeSelectionListener{
	JFrame frame;
	private JPanel contentPane;
	
	//tree
	private File root;
	private TreeModel modele;
	private JTree tree; 
	private JTextArea infos;

	//Menu
	private JMenu menu;
	
	void showFrame() {
		if (frame == null) {
			//fenetre entiere
			frame = new JFrame("Explorateur");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setBounds(200, 100, 450, 300);
			frame.setSize(800,600);
			contentPane = new JPanel();
			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			contentPane.setLayout(new BorderLayout(0, 0));
			frame.setContentPane(contentPane);
			buildExplorateur("D:/");
			buildMenu();
		}
		frame.setVisible(true);
	}
	
	@SuppressWarnings("serial")  
	void buildMenu() {  
		JMenuBar bar = new JMenuBar();  
		frame.setJMenuBar(bar);  
		JMenu fileM = new JMenu("Fichier");  
		JMenu fileM2 = new JMenu("Affichage");  
		bar.add(fileM);
		bar.add(fileM2);
		fileM.add(new AbstractAction("Save", new ImageIcon("res/save-icon16.png")) {  
			@Override  
			public void actionPerformed(ActionEvent arg0) {  
				System.out.println("DoSaveAction:"+arg0);  
			}  
			@Override  
			public Object getValue(String arg0) {  
				if(arg0==Action.ACCELERATOR_KEY) // cannot be changed later (use putValue when possible - not anonymous)  
					return KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK);  
				return super.getValue(arg0);  
			}  
		});
		
		//Ajout des sous menus pour l'affichage. On utilise le code de chaque sous menu qui est dans le package menu
		fileM2.add(new Details());
		fileM2.add(new PetitesMiniatures());
		fileM2.add(new GrandesMiniatures());
	}  
	
	void buildExplorateur(String repertoire){
		
		JSplitPane page = new JSplitPane();	
		JPanel vue = new JPanel();
		root = new File(repertoire);
		modele = new FileTreeModel(root);
		tree = new JTree(modele);
		tree.setCellRenderer(new FileRenderer());
		tree.addTreeSelectionListener(this);
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.setName("Dossiers");
		
		page.setRightComponent(vue);
		page.setLeftComponent(tree);
		contentPane.add(page);		
		
	}
	public void valueChanged(TreeSelectionEvent e){
		TreePath path = e.getPath();
		File file = (File)path.getLastPathComponent();
		String s = "Can read : \n   "+file.canRead()+"\n";
		s += "Can write : \n   "+file.canWrite()+"\n";
		s += "Parent : \n   "+file.getParent()+"\n";
		s += "Name : \n   "+file.getName()+"\n";
		s += "Length : \n   "+file.length()+"\n";
		s += "Last modified : \n   "+new Date(file.lastModified())+"\n";
		infos.setText(s);
	}
	
	public static void main(String[] args){
		new Explorateur().showFrame();
	}

}
